package hu.bme.mit.obp.seammodel.incquery.validation.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.viatra2.emf.incquery.validation.ui.editorlink.EditorBoundValidation;

import seample.presentation.SeampleEditor;

public class InitValidationActionDelegate implements IEditorActionDelegate {

	IEditorPart currentEditor;
	
	@Override
	public void run(IAction action) {
		
		// Beautiful and concise code to invoke BPELMultipageEditorPart.getProcess() on this.currentEditor		
		try {
			if(currentEditor instanceof SeampleEditor)
			{
				SeampleEditor seditor = (SeampleEditor) currentEditor;
//				Resource resource = null;
				EditorBoundValidation.INSTANCE.initializeValidatorsOnEditor(currentEditor, seditor.getEditingDomain().getResourceSet());
//				for (Resource res : seditor.getEditingDomain().getResourceSet().getResources()) {
//					if ("seammodel".equals(res.getURI().fileExtension())) {
//						resource = res; 
//					}
//				}
//				if (resource != null) {
//					System.err.println("Initializing validator on resource: " + resource.getURI());
//					EObject root = resource.getContents().get(0);
//					if (root!=null) {
//						try {
//							
//						} catch (IncQueryRuntimeException e) {
//							e.printStackTrace();
//						}
//					}
//				}
			}						
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
	}

	
//	boolean partlistenerRegistered = false;
	
	@Override
	public void setActiveEditor(IAction action, IEditorPart targetEditor) {

		this.currentEditor = targetEditor;
//		if (!partlistenerRegistered) {
//			registerPartListener(targetEditor);
//			partlistenerRegistered = true;
//		}
	}

//	private void registerPartListener(IEditorPart targetEditor) {
//			targetEditor.getEditorSite().getPage().addPartListener(new IPartListener2() {
//			
//			@Override
//			public void partVisible(IWorkbenchPartReference partRef) { }
//			
//			@Override
//			public void partOpened(IWorkbenchPartReference partRef) { }
//			
//			@Override
//			public void partInputChanged(IWorkbenchPartReference partRef) { }
//			
//			@Override
//			public void partHidden(IWorkbenchPartReference partRef) { }
//			
//			@Override
//			public void partDeactivated(IWorkbenchPartReference partRef) { }
//			
//			@Override
//			public void partClosed(IWorkbenchPartReference partRef) {
//				IWorkbenchPart part = partRef.getPart(false);
//				if (activeEditorRoots.keySet().contains(part)) {
//					EObject emfRoot = activeEditorRoots.get(part);
//					IFile file = activeEditorFiles.get(part);
//					ValidationUtil.closeValidators(emfRoot, file);
//					activeEditorRoots.remove(part);
//					activeEditorFiles.remove(part);
//				}
//			}
//			
//			@Override
//			public void partBroughtToTop(IWorkbenchPartReference partRef) { }
//			
//			@Override
//			public void partActivated(IWorkbenchPartReference partRef) { }
//		});
//
//	}

}
